using System;
using System.Net.Http;
using System.Threading.Tasks;
using CHEnhanceEfCore.Common;
using CHEnhanceEfCore.Config.Log;
using CHEnhanceEfCore.Models;
using CHEnhanceEfCore.Service;
using Microsoft.AspNetCore.Mvc;

namespace CHEnhanceEfCore.Controller
{
    /// <summary>
    /// author: chenhao
    /// </summary>
    public class LoginController : BaseApiController
    {
        private readonly IUserService _userService;

        private readonly LogUtils _logUtils;
        // private readonly INacosNamingService _svc;

        public LoginController(IUserService userService, LogUtils logUtils)
        {
            _userService = userService;
            _logUtils = logUtils;
            // _svc = svc;
        }

        [HttpPost("")]
        public async Task<Result> login(User user)
        {
            _logUtils.Info($"{user.username} is logged in {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            dynamic result = await _userService.loginAsync(user);
            return Result.success(result);
        }
        
        
        // //帮我写一个hello  nacos test
        // [HttpGet("hello")]
        // public async Task<string> hello()
        // {
        //     var instance = await _svc.SelectOneHealthyInstance("base-provider-hello", "DEFAULT_GROUP");
        //     var host = $"{instance.Ip}:{instance.Port}";
        //
        //     var baseUrl = instance.Metadata.TryGetValue("secure", out _)
        //         ? $"https://{host}"
        //         : $"http://{host}";
        //             
        //     if(string.IsNullOrWhiteSpace(baseUrl))
        //     {
        //         return "empty";
        //     }
        //
        //     var url = $"{baseUrl}/hello";
        //
        //     using HttpClient client = new HttpClient();
        //     var result = await client.GetAsync(url);
        //     return await result.Content.ReadAsStringAsync();
        // }
        //
        
    }
}